package com.yiyuan.common.search;

import java.io.File;

import org.apache.lucene.document.DateTools;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;

import com.yiyuan.common.util.FileUtil;

public class FileDocument {

	public static Document Document(DocObj docObj) throws Exception {

		File f = new File(docObj.getDocPath());
		Document doc = new Document();
		String fileName = docObj.getDocName();
		String ext = fileName.substring(fileName.lastIndexOf("."));
		ext = ext.toLowerCase();
		String content = null;
		if (ext.equals(".doc")) {
			content = FileUtil.getDOCContent(f);
		} else if (ext.equals(".xls")) {
			content = FileUtil.getXLSContent(f);
		} else if (ext.equals(".ppt")) {
			content = FileUtil.getPPTContent(f);
		} else if (ext.equals(".pdf")) {
			content = FileUtil.getPDFContent(f);
		} else if (ext.equals(".txt")) {
			content = FileUtil.getTXTContent(f);
		}
		doc.add(new Field("docId", docObj.getDocId(), Field.Store.YES,
				Field.Index.NOT_ANALYZED));
		doc.add(new Field("docName", docObj.getDocName(), Field.Store.YES,
				Field.Index.NOT_ANALYZED));
		doc.add(new Field("docPath", docObj.getDocPath(), Field.Store.YES,
				Field.Index.NOT_ANALYZED));
		doc.add(new Field("modified", DateTools.timeToString(f.lastModified(),
				DateTools.Resolution.MINUTE), Field.Store.YES,
				Field.Index.NOT_ANALYZED));
		doc.add(new Field("docDesc", docObj.getDocDesc(), Field.Store.YES,
				Field.Index.NOT_ANALYZED));
		doc.add(new Field("docContent", content, Field.Store.NO,
				Field.Index.ANALYZED));
		return doc;

	}

	private FileDocument() {
	}

}
